package xuesm.prototype.deep;

import java.io.*;

/**
 * @Author: xuesm
 * @Description: 深克隆
 * @Date: Create in 20:20 2019/4/1
 */
public class UserDeepPrototype implements Serializable, Cloneable{

	private int age;
	private String name;

	private Wife wife;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Wife getWife() {
		return wife;
	}

	public void setWife(Wife wife) {
		this.wife = wife;
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		return this.deepClone();
	}

	// 深克隆
	private Object deepClone() {


		try {
			// 序列化
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(bos);
			oos.writeObject(this);
			bos.flush();
			oos.close();

			// 反序列化
			ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
			ObjectInputStream ois = new ObjectInputStream(bis);
			return ois.readObject();


		}catch (Exception e){

		}

		return null;
	}
}
